import "reflect-metadata"; // 装饰器的基础，放在顶层
import { InversifyExpressServer } from "inversify-express-utils";
import { Container } from "inversify";
import { UserController } from "./src/user/controller";
import { UserService } from "./src/user/services";
import express from "express";
import { PrismaClient } from "@prisma/client";
import { PrismaDB } from "./src/db";

const container = new Container();
// 注入工厂注入PrismaClient
container.bind<PrismaClient>("PrismaClient").toFactory(() => {
  return () => new PrismaClient();
});
// 注入数据库ORM框架PrismaClient
container.bind(PrismaDB).to(PrismaDB);

// 注入User服务
container.bind(UserController).to(UserController);
container.bind(UserService).to(UserService);

const server = new InversifyExpressServer(container);
// 配置中间件,允许post参数
server.setConfig((app) => {
  app.use(express.json());
});
const app = server.build();

app.listen(3000, () => {
  console.log("Server is running on port 3000");
});
